<?php

class SearchHistory extends CApplicationComponent
{
	public $clients = array();
	public $credits = array();

	public function addClientEntry($id,$fio)
	{
		if(Yii::app()->user->hasState('clientsSearch'))
			$this->clients = unserialize(Yii::app()->user->getState('clientsSearch'));

		$is = 0;
		foreach($this->clients as $client){
			if($client['id']==$id){
				$is = 1;
				break;
			}
		}

		if(!$is){
			array_unshift($this->clients,array('id'=>$id,'fio'=>$fio));
			$this->clients = array_slice($this->clients,0,10);

			Yii::app()->user->setState('clientsSearch',serialize($this->clients));
		}
	}

	public function getClientsHistory()
	{
		if(Yii::app()->user->hasState('clientsSearch'))
			return unserialize(Yii::app()->user->getState('clientsSearch'));
		else
			return array();
	}

	public function addCreditEntry($id)
	{
		if(Yii::app()->user->hasState('creditsSearch'))
			$this->credits = unserialize(Yii::app()->user->getState('creditsSearch'));

		if(!in_array($id,$this->credits)){
			array_unshift($this->credits,$id);
			$this->credits = array_slice($this->credits,0,10);
		}

		Yii::app()->user->setState('creditsSearch',serialize($this->credits));
	}

	public function getCreditsHistory()
	{
		if(Yii::app()->user->hasState('creditsSearch'))
			return unserialize(Yii::app()->user->getState('creditsSearch'));
		else
			return array();
	}
}